<script setup lang="ts">
import { createDeviceThreshold, getDeviceInfoList } from '@/api/device';
import type { DeviceInfo, DeviceThreshold } from '@/api/device/type';
import type { Result } from '@/utils/request';
import { ElMessage } from 'element-plus';
import { onMounted, ref } from 'vue';

const emit = defineEmits(["refresh"]);
const dialogFormVisible = defineModel();
const formData = ref({} as DeviceThreshold);
const deviceList = ref([] as DeviceInfo[]);
const selectDevice = ref('');
onMounted(() => {
    getDeviceInfoList().then((res) => {
        deviceList.value = res.data;
    })
})
const onSubmit = () => {
    formData.value.deviceId = selectDevice.value;
    createDeviceThreshold(formData.value).then((res) => {
        const {msg} = res as Result;
        ElMessage({
            message: msg,
            type: 'success'
        })
        dialogFormVisible.value = false;
        emit("refresh");
    });
}
</script>

<template>
    <div>
        <el-dialog v-model="dialogFormVisible" title="创建阈值" width="500">
            <el-form :model="formData">
                <el-form-item label="设备">
                    <el-select
                        v-model="selectDevice"
                        placeholder="选择设备"
                        size="large"
                        style="width: 240px"
                    >
                        <el-option
                            v-for="(item, index) in deviceList"
                            :key="index"
                            :label="item.name"
                            :value="item.id"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="温度上限">
                    <el-input v-model="formData.temperatureUpper" autocomplete="off" />
                </el-form-item>
                <el-form-item label="温度下限">
                    <el-input v-model="formData.temperatureLower" autocomplete="off" />
                </el-form-item>
                <el-form-item label="湿度上限">
                    <el-input v-model="formData.humidityUpper" autocomplete="off" />
                </el-form-item>
                <el-form-item label="湿度下限">
                    <el-input v-model="formData.humidityLower" autocomplete="off" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取消</el-button>
                    <el-button type="primary" @click="onSubmit">提交</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<style lang="scss" scoped>

</style>